package com.heima.user.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.common.constants.UserConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUserFan;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.mapper.ApUserFanMapper;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.service.UserFollowService;
import com.heima.utils.thread.WeMediaThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class UserFollowServiceImpl implements UserFollowService {
    @Autowired
    private CacheService cacheService;
    @Autowired
    private ApUserFollowMapper apUserFollowMapper;
    @Autowired
    private ApUserFanMapper apUserFanMapper;


    /**
     * 关注与取消关注
     *
     * @param dto
     */
    @Override
    public ResponseResult follow(UserRelationDto dto) {
        //1.参数校验
        if(dto == null || (dto.getOperation() != 0 && dto.getOperation() != 1) || dto.getArticleId() == null || dto.getAuthorId() == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        //2.业务处理
        //2.1获取userId
        Integer userId = WeMediaThreadLocalUtil.get();
        if(userId == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        //2.2更新数据到redis
        if(dto.getOperation() == 0){//关注
            //保存数据
            cacheService.zAdd(UserConstants.APUSER_FOLLOW + userId.toString(),dto.getAuthorId().toString(),System.currentTimeMillis());   //关注表
            cacheService.zAdd(UserConstants.APUSER_FANS + dto.getAuthorId().toString(),userId.toString(),System.currentTimeMillis());   //粉丝表
        }else {//取消关注
            //删除数据
            cacheService.zRemove(UserConstants.APUSER_FOLLOW + userId.toString(),dto.getAuthorId().toString());
            cacheService.zRemove(UserConstants.APUSER_FANS + dto.getAuthorId().toString(),userId.toString());
        }

        //2.3更新数据到数据库
        if(dto.getOperation() == 0){//关注
            //创建关注对象
            ApUserFollow apUserFollow = new ApUserFollow();
            apUserFollow.setUserId(userId);
            apUserFollow.setFollowId(dto.getAuthorId());
            apUserFollow.setCreatedTime(new Date());
            apUserFollowMapper.insert(apUserFollow);
            //创建粉丝对象
            ApUserFan apUserFan = new ApUserFan();
            apUserFan.setUserId(dto.getAuthorId());
            apUserFan.setFansId(userId);
            apUserFan.setCreatedTime(new Date());
            apUserFanMapper.insert(apUserFan);

        }else {//取消关注
            //删除关注表中信息
            LambdaQueryWrapper<ApUserFollow> follow = Wrappers.<ApUserFollow>lambdaQuery()
                    .eq(ApUserFollow::getUserId, userId)
                    .eq(ApUserFollow::getFollowId, dto.getAuthorId());
            ApUserFollow apUserFollow = apUserFollowMapper.selectOne(follow);
            if(apUserFollow == null)
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
            apUserFollowMapper.delete(follow);
            //删除粉丝表中信息
            LambdaQueryWrapper<ApUserFan> fan = Wrappers.<ApUserFan>lambdaQuery()
                    .eq(ApUserFan::getUserId, dto.getAuthorId())
                    .eq(ApUserFan::getFansId, userId);
            ApUserFan apUserFan = apUserFanMapper.selectOne(fan);
            if(apUserFan == null)
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
            apUserFanMapper.delete(fan);

        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
